package com.ruoyi.article.service.impl;

import com.ruoyi.article.domain.Article;
import com.ruoyi.article.mapper.ArticleMapper;
import com.ruoyi.article.service.ArticleService;
import com.ruoyi.common.core.constant.ArticleConstants;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.net.URLDecoder;
import java.util.List;

@Service
public class ArticleServiceImpl implements ArticleService {

    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public boolean checkArticleUnique(Article article) {
        Long articleId =  StringUtils.isNull(article.getArticleId()) ? -1 : article.getArticleId();
        Article articleCheck =  articleMapper.checkArticleUnique(article.getName());
        if (StringUtils.isNotNull(articleCheck) && articleCheck.getArticleId().longValue() != articleId.longValue()) {
            return ArticleConstants.NOT_UNIQUE;
        }
        return ArticleConstants.UNIQUE;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertArticle(Article article) {

        String decodedValue = "";
        String content = article.getContent();
        try{
            decodedValue = URLDecoder.decode(content, "UTF-8");
        }catch (Exception e){
            return 0;
        }
        article.setContent(decodedValue);

        return articleMapper.insertArticle(article);
    }

    @Override
    public List<Article> selectArticleList(Article article) {
        return articleMapper.selectArticleList(article);
    }
    @Override
    public Article selectArticleById(Long articleId) {
        return articleMapper.selectArticleById(articleId);
    }


    @Override
    public int deleteArticleByIds(Long[] articleIds) {
        return articleMapper.deleteArticleByIds(articleIds);
    }

    @Override
    public int deleteArticleById(Long articleId) {
        return articleMapper.deleteArticleById(articleId);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateArticle(Article article){
        String decodedValue = "";
        String content = article.getContent();
        try{
            decodedValue = URLDecoder.decode(content, "UTF-8");
        }catch (Exception e){
            return 0;
        }
        article.setContent(decodedValue);
        return articleMapper.updateArticle(article);
    }


//    @Override
//    @Transactional(rollbackFor = Exception.class)
//    public int deleteUserById(Long userId)
//    {
//        // 删除用户与角色关联
//        commodityMapper.deleteUserRoleByUserId(userId);
//        // 删除用户与岗位表
//        commodityMapper.deleteUserPostByUserId(userId);
//        return commodityMapper.deleteUserById(userId);
//    }
}
